package com.richrail.console.commands;

import com.richrail.model.PrefixedNameGenerator;
import com.richrail.model.WagonFactory;
import com.richrail.model.entities.DepotStorage;
import com.richrail.model.entities.Wagon;

public class NewWagonCommand
{
	private WagonFactory factory;
	
	private DepotStorage depotStorage;
	
	public NewWagonCommand()
	{
		factory = new WagonFactory(new PrefixedNameGenerator("wg"));
	}
	
	public void setDepotStorage(DepotStorage depotStorage)
	{
		this.depotStorage = depotStorage;
	}
	
	// We make use of an Integer object instead of the primitive int,
	// because we need to tell the difference between null and 0.
	// Since the are cases where you might have a seat count of 0,
	// which should not be converted to the default seat count (20 for example).
	public String execute(String name, Integer numSeats)
	{
		Wagon wagon;
		
		if (numSeats == null)
		{
			wagon = factory.create(name);
		}
		else
		{
			if (numSeats < 0)
			{
				return "could not create wagon, negative number of seats given";
			}
			
			wagon = factory.create(name, numSeats);
		}
		
		return depotStorage.addWagon(wagon)
			? "wagon " + wagon + " created"
			: "wagon " + wagon + " already exists";
	}
}
